package com.ruoyi.web.utils;

import com.ruoyi.web.domain.LegalTender;
import com.ruoyi.web.domain.QgPayBank;
import com.ruoyi.web.domain.QgPayBusiness;
import com.ruoyi.web.enums.CommonDictEnum;
import com.ruoyi.web.mapper.LegalTenderMapper;
import com.ruoyi.web.mapper.QgPayBankMapper;
import com.ruoyi.web.mapper.QgPayBusinessMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * redis缓存工具类
 *
 * @since 2023/3/16 14:48
 */
@Slf4j
@Component
@AllArgsConstructor
public class RedisCacheUtil {
    private final LegalTenderMapper legalTenderMapper;
    private final QgPayBusinessMapper qgPayBusinessMapper;
    private final QgPayBankMapper qgPayBankMapper;

    /**
     * 获取法币
     *
     * @param legalTenderCode 法币编码
     * @return {@link LegalTender}
     * @since 2023/3/16 14:50
     */
    public LegalTender getLegalTender(String legalTenderCode, boolean nullable) {
        LegalTender condition = new LegalTender();
        condition.setCode(legalTenderCode);
        condition.setStatus(CommonDictEnum.Status.NORMAL.getValue());
        List<LegalTender> list = legalTenderMapper.selectLegalTenderList(condition);
        if (CollectionUtils.isEmpty(list) && !nullable) {
            log.error("未能查询到法币信息，legalTenderCode：{}", legalTenderCode);
            CommonUtil.throwError();
        }

        return list.get(0);
    }

    /**
     * 获取QGPay业务
     *
     * @param businessCode 业务编码
     * @return {@link QgPayBusiness}
     * @since 2023/3/16 14:50
     */
    public QgPayBusiness getQgPayBusiness(String businessCode, boolean nullable) {
        QgPayBusiness condition = new QgPayBusiness();
        condition.setCode(businessCode);
        condition.setStatus(CommonDictEnum.Status.NORMAL.getValue());
        List<QgPayBusiness> list = qgPayBusinessMapper.selectQgPayBusinessList(condition);
        if (CollectionUtils.isEmpty(list) && !nullable) {
            log.error("未能查询到QGPay业务信息，businessCode：{}", businessCode);
            CommonUtil.throwError();
        }

        return list.get(0);
    }

    /**
     * 获取QGPay银行
     *
     * @param bankCode 银行编码
     * @return {@link QgPayBank}
     * @since 2023/3/16 14:50
     */
    public QgPayBank getQgPayBank(String bankCode, boolean nullable) {
        QgPayBank condition = new QgPayBank();
        condition.setCode(bankCode);
        condition.setStatus(CommonDictEnum.Status.NORMAL.getValue());
        List<QgPayBank> list = qgPayBankMapper.selectQgPayBankList(condition);
        if (CollectionUtils.isEmpty(list) && !nullable) {
            log.error("未能查询到QGPay银行信息，bankCode：{}", bankCode);
            CommonUtil.throwError();
        }

        return list.get(0);
    }
}
